package com.zs.grain.ui

import android.os.Bundle
import androidx.lifecycle.Observer
import com.alibaba.android.arouter.facade.annotation.Route
import com.jeremyliao.liveeventbus.LiveEventBus

import com.zs.grain.*
import com.zs.grain.databinding.FragmentScanQrBinding
import com.zs.grain.service.SoundService

import com.xnkj1688.semplibrary.mvvm.*
import com.zs.grain.tool.clockManage.LocalDataBase

/**
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 */
@Route(path = ARouterPath.ScanQrFragment)
class ScanQrFragment : BaseMvvmFragment<FragmentScanQrBinding, ScanQrViewModel>() {
    override val layoutId = R.layout.fragment_scan_qr

    override fun initViewModel(binding: FragmentScanQrBinding) {
        binding.vm = this.mViewModel
        binding.view = this
    }

    override fun initCreate() {
        LiveEventBus.get("ScanQr", Bundle::class.java).observe(this, Observer {
            var orderId = it.getLong("OrderId")
            var goodId = it.getLong("goodId")
            var sealIndex = this.arguments?.getInt("sealIndex")
            var sugarIndex = this.arguments?.getInt("sugarIndex")
            mViewModel.init(orderId,goodId!!,sealIndex!!,sugarIndex!!)
        })
        LiveEventBus.get("SignalR.Payed", Bundle::class.java).observe(this, Observer {
            var orderId = it.getLong("OrderId")
            var payWay = it.getInt("PayWay")
            mViewModel.checkPayment(orderId, payWay)
        })
    }

    override fun initArguments() {
        var orderId = this.arguments?.getLong("OrderId")
        var goodId = this.arguments?.getLong("goodId")
        var sealIndex = this.arguments?.getInt("sealIndex")
        var sugarIndex = this.arguments?.getInt("sugarIndex")
        if (orderId != null) mViewModel.init(orderId,goodId!!,sealIndex!!,sugarIndex!!)
    }

    override fun onResume() {
        mViewModel.mDataService.insertLog("State:进入支付页面")
        super.onResume()
        //this.mViewModel.mSoundService.playSound2(3)
    }

    override fun onPause() {
        mViewModel.mDataService.insertLog("State:退出支付页面")
        super.onPause()
        mViewModel.stopCheckPaymentTimer()
        this.mViewModel.mSoundService.stopSound2()
    }

    override fun onUiNotify(name: String, bundle: Bundle) {
        when (name) {
            "Failture" -> {
                var loading = "res://${this.activity!!.packageName}/${R.mipmap.loading}"
                var failture = "res://${this.activity!!.packageName}/${R.mipmap.failture}"
                if (this.mViewModel.alipay.value == loading) this.mViewModel.alipay.value = failture
                if (this.mViewModel.weixin.value == loading) this.mViewModel.weixin.value = failture
                if (this.mViewModel.app.value == loading) this.mViewModel.app.value = failture
            }
            "Completed" -> {
                var loading = "res://${this.activity!!.packageName}/${R.mipmap.loading}"
                var failture = "res://${this.activity!!.packageName}/${R.mipmap.failture}"
                if (this.mViewModel.alipay.value == loading) this.mViewModel.alipay.value = failture
                if (this.mViewModel.weixin.value == loading) this.mViewModel.weixin.value = failture
                if (this.mViewModel.app.value == loading) this.mViewModel.app.value = failture
            }
            "Payed" -> {
                mViewModel.stopCheckPaymentTimer()
                var bundle = Bundle()
                bundle.putString("UI", "Waiting")
                bundle.putLong("OrderId", mViewModel.order.id)
                LiveEventBus.get("SwitchUI").post(bundle)
            }
            "Cancel" -> {
                mViewModel.stopCheckPaymentTimer()
                this.mViewModel.mDeviceService.isMakingProduct = false
                //this.mViewModel.mDataService.insertLog("SVM")
                var bundle = Bundle()
                bundle.putString("UI", "Menus")
                LiveEventBus.get("SwitchUI").post(bundle)
            }
            "Timeout" -> {
                mViewModel.stopCheckPaymentTimer()
                var bundle = Bundle()
                bundle.putString("UI", "Menus")
                LiveEventBus.get("SwitchUI").post(bundle)
            }
        }
        super.onUiNotify(name, bundle)
    }

    companion object {
        fun newInstance() = ScanQrFragment()
    }
}
